﻿#nullable disable
using IMES.BLL.Dtos;
using System.Net;

namespace IMES.Utils
{
    /// <summary>
    /// 本机计算机-Helper
    /// </summary>
    public class LocalComputerHelper
    {
        private static LocalComputerHelper manager=new LocalComputerHelper ();
        private ComputerInfo computerInfo=null;
        public static LocalComputerHelper Instance
        { get { return manager; } }

        public LocalComputerHelper()
        {
            if (computerInfo == null) computerInfo = GetComputerInfo();
        }

        /// <summary>
        /// 当前计算机信息
        /// </summary>
        public ComputerInfo ComputerInfo
        {
            get 
            { 
                return computerInfo; 
            }
        }

        /// <summary>
        /// 缓存-登录账号
        /// </summary>
        /// <param name="uinfo">用户信息</param>
        public void CacheLoginAccount(UserVO uinfo)
        {
            computerInfo.User= uinfo;
        }

        /// <summary>
        /// 获取计算机信息
        /// </summary>
        /// <returns></returns>
        public  ComputerInfo GetComputerInfo()
        {
            if (computerInfo == null)
            {
                computerInfo = new ComputerInfo(); 
                computerInfo.MachineName = System.Environment.MachineName;
                computerInfo.DomainName = System.Environment.UserName;
                //程序版本
                Version? sysVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
                string versionfull = (sysVersion != null) ? sysVersion.ToString() : "";
                List<string> versionCodes = versionfull.Split(".", StringSplitOptions.RemoveEmptyEntries).ToList();
                computerInfo.AppVersion = "版本:" + versionCodes[0] + "." + versionCodes[1] + "." + versionCodes[2];
                //IP地址
                List<string> iplist = GetLocalIpAddress("InterNetwork");
                if (iplist != null && iplist.Count > 0)
                {
                    computerInfo.IPAddress = iplist[0];
                }
            }
            return computerInfo;
        }

        /// 获取本机所有ip地址
        /// </summary>
        /// <param name="netType">"InterNetwork":ipv4地址，"InterNetworkV6":ipv6地址</param>
        /// <returns>ip地址集合</returns>
        private List<string> GetLocalIpAddress(string netType)
        {
            string hostName = Dns.GetHostName();                    //获取主机名称
            IPAddress[] addresses = Dns.GetHostAddresses(hostName); //解析主机IP地址

            List<string> IPList = new List<string>();
            if (netType == string.Empty)
            {
                for (int i = 0; i < addresses.Length; i++)
                {
                    IPList.Add(addresses[i].ToString());
                }
            }
            else
            {
                //AddressFamily.InterNetwork表示此IP为IPv4,
                //AddressFamily.InterNetworkV6表示此地址为IPv6类型
                for (int i = 0; i < addresses.Length; i++)
                {
                    if (addresses[i].AddressFamily.ToString() == netType)
                    {
                        IPList.Add(addresses[i].ToString());
                    }
                }
            }
            return IPList;
        }
    }

    /// <summary>
    /// 计算机信息
    /// </summary>
    public class ComputerInfo
    {
        /// <summary>
        /// 计算机名
        /// </summary>
        public string MachineName { get; set; }
        /// <summary>
        /// 域账号
        /// </summary>
        public string DomainName { get; set; }
        /// <summary>
        /// IP地址
        /// </summary>
        public string IPAddress {  get; set; }
        /// <summary>
        /// App版本号
        /// </summary>
        public string AppVersion {  get; set; }
        /// <summary>
        /// 用户信息
        /// </summary>
        public UserVO User { get; set; }
    }
}
